package com.zb.spring5;

import com.zb.spring5.configuration.AppConfigOne;
import com.zb.spring5.ioc.entity.Singer;
import com.zb.spring5.ioc.inteface.MessageRender;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

public class MainTest {

    public static void main(String[] args) throws IOException {

//        StandarOutMessagerRender mr = new StandarOutMessagerRender();
//        MessagePrividerImpl messagePrivider = new MessagePrividerImpl();
//        mr.setMessagePrivider(messagePrivider);
//        mr.render();

        /**
         * 演示通过属性文件注册bean
         */
//        MessagePrivider privider = MessageSupportFactory.getInstance().getPrivider();
//        MessageRender render = MessageSupportFactory.getInstance().getRender();
//        render.setMessagePrivider(privider);
//        render.render();
        /**
         * 继续重构,引入spring的包之后可以这么干
         */
//        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/app-content.xml");
//        MessageRender render = ctx.getBean("renderer", MessageRender.class);
//        render.render();

        /**
         * 使用注解方式注入bean
         */
//        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(TestConfiguration.class);
//        MessageRender render = ctx.getBean("render", MessageRender.class);
//        render.render();
        /**
         * 使用注解,扫描格式注入容器
         */
//        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
//        ctx.load("classpath:spring/app-content-xml.xml");
//        ctx.refresh();
//        MessageRender render = ctx.getBean("render", MessageRender.class);
//        render.render();
//        ctx.close();
        /**
         * 不用配置文件,使用注解扫描注入@componentScan
         */
//        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigurationScan.class);
//        MessageRender render1 = ctx.getBean("render", MessageRender.class);
//        render1.render();
//        System.out.println(render1.getMessagePrivider().getMessage());
        /**
         * bean的生命周期
         */
//        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
//        ctx.load("classpath:spring/app-content-xml.xml");
//        ctx.refresh();
//        getBean("singerOne",ctx);
//        getBean("singerTwo",ctx);
//        getBean("singerThree",ctx);

//        GenericApplicationContext ctx = new AnnotationConfigApplicationContext(SingerConfig.SingerConfigDemo.class);
//        getBean("singerOne",ctx);
//        getBean("singerTwo",ctx);
//        getBean("singerThree",ctx);
//        NamedSinger bean = (NamedSinger)ctx.getBean("johnMayer");
//        bean.sing();
//        ctx.close();
//        MessageDigester digester = ctx.getBean("digester", MessageDigester.class);
//        digester.digest("hello world");
        /**
         * 国际化
         */
//        Locale english = Locale.ENGLISH;
//        Locale chinese = Locale.SIMPLIFIED_CHINESE;
//        System.out.println(ctx.getMessage("msg",null,english));
//        System.out.println(ctx.getMessage("msg",null,chinese));
//        System.out.println(ctx.getMessage("msgName",new Object[]{"bo","zhang"},english));
//        System.out.println(ctx.getMessage("msgName",new Object[]{"bo","zhang"},chinese));
        /**
         * 资源  文件资源加载, 类文件资源加载 , http 使用Url资源加载  查看资源内容最好使用getInputStream()
         */
//        ApplicationContext ctx = new ClassPathXmlApplicationContext();
//        File tempFile = File.createTempFile("text", "txt");
//        tempFile.deleteOnExit();
//        Resource resource = ctx.getResource("file://" + tempFile.getPath());
//        displayInfo(resource);
//        Resource resource1 = ctx.getResource("classpath:" + tempFile.getPath());
//        displayInfo(resource1);
//        Resource resource2 = ctx.getResource("http://www.baidu.com");
//        displayInfo(resource2);
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfigOne.class);
        MessageRender render = ctx.getBean("messageRender", MessageRender.class);
        render.render();
    }
    public static void displayInfo(Resource resource) throws IOException {
        System.out.println(resource.getClass());
        System.out.println(resource.getURL().getContent());
        System.out.println("");
    }
    public static Singer getBean(String beanName, ApplicationContext ctx){

        try {
            Singer bean = (Singer) ctx.getBean(beanName);
            System.out.println(bean);
            return bean;
        } catch (BeanCreationException e) {
            System.out.println("获取bean异常:"+e.getMessage());
            return null;
        }
    }
}
